<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🚀 系统监控面板</title>
    <style>
        body {
            margin: 0;
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f6f8;
        }

        /* 顶部状态栏 */
        .status-bar {
            background-color: #ffffff;
            color: #333;
            padding: 14px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 999;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .status-title {
            font-size: 18px;
            font-weight: bold;
        }

        .dropdown {
            position: relative;
            display: inline-block;
        }

        .dropdown-btn {
            background: none;
            border: none;
            color: #333;
            font-size: 16px;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .dropdown-content {
            display: none;
            position: absolute;
            right: 0;
            background-color: #2C2230;
            min-width: 120px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
            border-radius: 5px;
            overflow: hidden;
            z-index: 1000;
        }

        .dropdown-content a {
            color: white;
            padding: 10px 15px;
            text-decoration: none;
            display: block;
            font-size: 14px;
        }

        .dropdown-content a:hover {
            background-color: #3A2B45;
        }

        .dropdown:hover .dropdown-content {
            display: block;
        }

        /* 页面布局 */
        .main {
            display: flex;
            margin-top: 60px;
            height: calc(100vh - 60px);
        }

        /* 左侧栏 */
        .sidebar {
            width: 220px;
            background-color: #f0f0f0;
            color: black;
            padding: 20px;
            box-shadow: 2px 0 5px rgba(0, 0, 0, 0.2);
        }

        .sidebar a {
            display: block;
            padding: 12px 20px;
            color: #333333;
            text-decoration: none;
            font-size: 15px;
        }

        .sidebar a:hover {
            background-color: #f0f0f0;
        }

        /* 右侧内容 */
        .content {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
        }

        .card {
            background: #fff;
            border-radius: 4px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        }

        .card h2 {
            margin-top: 0;
            color: #606060;
            font-size: 16px;
        }

        .card div {
            margin-top: 0;
            color: #606060;
            font-size: 18px;
        }

        .data-value {
            font-size: 28px;
            font-weight: bold;
            margin-top: 8px;
        }

        .ip {
            background-color: #fff3e0;
        }

        .time {
            background-color: #e3f2fd;
        }

        .uptime {
            background-color: #e8f5e9;
        }

        .cpu {
            color: #ff9800;
        }

        .mem {
            color: #2196f3;
        }
    </style>
</head>

<body>

    <!-- 顶部状态栏 -->
    <div class="status-bar">
        <div class="status-title">🚀 系统监控面板</div>
        <div>
            <div class="dropdown">
                <button class="dropdown-btn">⚙️ 系统</button>
                <div class="dropdown-content">
                    <a href="#" onclick="shutdown()">🔌 关机</a>
                    <a href="#" onclick="restart()">♻️ 重启</a>
                </div>
            </div>
        </div>
    </div>

    <!-- 主体 -->
    <div class="main">
        <!-- 左侧栏 -->
        <div class="sidebar">
            <div class="card ip">
                <h2>IP</h2>
                <div id="ipData" class="data-value">192.168.9.1</div>
            </div>
            <div class="card time">
                <h2>TIME</h2>
                <div id="timeData" class="data-value time">12:20:30</div>
            </div>
            <div class="card uptime">
                <h2>UPTIME</h2>
                <div id="uptimeData" class="data-value uptime">01:34:10</div>
            </div>
        </div>

        <!-- 右侧内容 -->
        <div class="content">
            <div class="card">
                <h2>CPU</h2>
                <div id="cpuData" class="data-value cpu">加载中...</div>
            </div>
            <div class="card">
                <h2>Mem</h2>
                <div id="memData" class="data-value mem">加载中...</div>
            </div>
        </div>
    </div>

    <script>
        function shutdown() {
            fetch("/api/shutdown", {method: "POST"})
                .then(res => res.json())
                .then(data => alert(`🔌 系统正在关机: ${data.status || '已发送关机命令'}`))
                .catch(err => alert("❌ 关机失败: " + err));
        }

        function restart() {
            fetch("/api/reboot", {method: "POST"})
                .then(res => res.json())
                .then(data => alert(`♻️ 系统正在重启: ${data.status || '已发送重启命令'}`))
                .catch(err => alert("❌ 重启失败: " + err));
        }

        // 获取 CPU & 内存数据
        function updateData() {
            fetch("/api/cpu")
                .then(res => res.json())
                .then(data => {
                    document.getElementById("cpuData").innerText = data.usage ? data.usage + "%" : "❓ 无数据";
                })
                .catch(() => document.getElementById("cpuData").innerText = "⚠️ 获取失败");

            fetch("/api/mem")
                .then(res => res.json())
                .then(data => {
                    document.getElementById("memData").innerText = data.usage ? data.usage + "%" : "❓ 无数据";
                })
                .catch(() => document.getElementById("memData").innerText = "⚠️ 获取失败");
        }

        // 每 5 秒刷新一次
        setInterval(updateData, 5000);
        updateData();
    </script>

</body>

</html>
